package com.coders.android.user;

import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

import com.coders.android.R;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;

public class UserMapActivity extends FragmentActivity {

	private final String TAG = this.getClass().getSimpleName();
	private final float ZOOM = 16;
	
	private EditText userMapGetTaxi;
	
	private String IMEI;
	private GoogleMap googleMap;
	
	MyAddress myAddress;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_user_map);
		
		initComponents();
	}
	
	private void initComponents() {
		
		userMapGetTaxi = (EditText) findViewById( R.id.activity_user_map_button_get_taxi );
		userMapGetTaxi.setOnClickListener( new GetTaxi() );
		
		googleMap = ( (SupportMapFragment) getSupportFragmentManager().findFragmentById( R.id.user_map ) ).getMap();
		
		googleMap.setMapType( GoogleMap.MAP_TYPE_NORMAL );
		googleMap.setMyLocationEnabled( true );

		LocationManager locationManager = null;
		locationManager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
		
		Location location = null;
		if( locationManager.isProviderEnabled( LocationManager.NETWORK_PROVIDER ) )
			 location = locationManager.getLastKnownLocation( LocationManager.NETWORK_PROVIDER );
		else if( locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER ) )
			 location = locationManager.getLastKnownLocation( LocationManager.GPS_PROVIDER );
		else {
			Toast.makeText( getBaseContext(), "GPS y WiFi no disponibles", Toast.LENGTH_LONG ).show();
			return;
		}
		
		/*googleMap.addMarker( new MarkerOptions().position( gpsLatLng ) );
		googleMap.addMarker( new MarkerOptions().position( wifiLatLng ) );*/
		
		googleMap.animateCamera( CameraUpdateFactory.newLatLngZoom( new LatLng( location.getLatitude(), location.getLongitude() ), ZOOM ) );
		
		TelephonyManager telephonyManager = (TelephonyManager) getSystemService( Context.TELEPHONY_SERVICE );
		IMEI = telephonyManager.getDeviceId();
		
		myAddress = new MyAddress();
		myAddress.execute( location.getLatitude(), location.getLongitude() );
	}
	
	class GetTaxi implements OnClickListener {

		@Override
		public void onClick(View v) {
			
		}
	}
	
	private class MyAddress extends AsyncTask<Double, Void, String> {

		@Override
		protected String doInBackground(Double... params) {

			Geocoder geocoder = new Geocoder( getBaseContext() );
			List<Address> addresses = null;
			
			try {
				Thread.sleep( 7000 );
				addresses = geocoder.getFromLocation( params[0], params[1], 1 );
				
				if( addresses != null && addresses.size() > 0 ) {
					
	                Address address = addresses.get(0);
	                return String.format(
	                		"%s, %s, %s",
	                        address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
	                        address.getLocality(),
	                        address.getCountryName()
	                        );
				}
			} catch (IOException e) {
				Log.e( TAG, e.getMessage() );
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			Toast.makeText( getBaseContext(), result, Toast.LENGTH_LONG ).show();
		}	
    }
}
